package com.juyu.miliao.api.controller;


import com.juyu.miliao.api.domain.Vip;
import com.juyu.miliao.api.domain.dto.CompenstateUserVipDTO;
import com.juyu.miliao.api.service.VestRobotMessageService;
import com.juyu.miliao.api.service.VipService;
import com.juyu.miliao.api.util.CompenstateUserVipUtil;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.constant.SpendSystemMessage;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.juyu.miliao.api.service.UserVipService;

import java.io.IOException;

/**
 * 用户vip信息
 *
 * @author wbf
 */
@RestController
@RequestMapping("/userVip")
@Slf4j
public class UserVipController {

	@Autowired
	UserVipService userVipService;

	@Autowired
    private VipService vipService;

    @Autowired
    private VestRobotMessageService vestRobotMessageService;

//	/**
//	 * 获取用户vip信息,前端暂时没用到
//	 *
//	 * @return
//	 */
//	@GetMapping("/{userId:^[1-9][0-9]*$}")
//	public UserVip getUserVipById(@PathVariable("userId") Integer userId) {
//		return userVipService.getUserVipById(userId);
//	}


    @PostMapping("/compensate")
    public void compenstateUserVip(@Validated CompenstateUserVipDTO compenstateUserVipDTO,
                                   BindingResult bindingResult) throws IOException {

        log.info("UserVipController请求参数:[{}]",compenstateUserVipDTO);
        if(bindingResult.hasErrors()){
            log.error("UserVipController参数校验不通过:[{}],参数:[{}]",bindingResult.getFieldError().getDefaultMessage(),
                    compenstateUserVipDTO);
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        boolean bl = CompenstateUserVipUtil.checkCompenstateUserVipSigne(compenstateUserVipDTO);

        if(!bl){
            log.error("UserVipController签名不正确:[{}]",compenstateUserVipDTO);
            throw  new ConsumeRuntimeException("签名不正确");
        }

        userVipService.changeUserVip(compenstateUserVipDTO.getUserId(),compenstateUserVipDTO.getVipId(),
                compenstateUserVipDTO.getContentType(),compenstateUserVipDTO.getPayNum());

        //发送系统消息
        Vip vip = vipService.getVipById(compenstateUserVipDTO.getVipId());

        int  validTime = vip.getValidTime();
        //普通vip
        String message = "";
        if(compenstateUserVipDTO.getContentType() == 2){
            message = SpendSystemMessage.COMPENSATE_VIP_SEND_MESSAGE.getMessage(validTime);
        }else if(compenstateUserVipDTO.getContentType() == 3){
            message = SpendSystemMessage.COMPENSATE_SVIP_SEND_MESSAGE.getMessage(validTime);
        }else if(compenstateUserVipDTO.getContentType() == 4){
            message = SpendSystemMessage.COMPENSATE_PLATINUM_VIP_SEND_MESSAGE.getMessage(validTime);
        }
        vestRobotMessageService.getSendMsgBySystem(compenstateUserVipDTO.getUserId(),message);
    }

}
